/**
 * Copyright &copy; 2015-2020 <a href="www.dsesa.cn/">ESA</a> All rights reserved.
 */
package com.jeeplus.modules.sys.service;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sys.entity.ArchCostItem;
import com.jeeplus.modules.sys.entity.DictType;
import com.jeeplus.modules.sys.entity.DictValue;
import com.jeeplus.modules.sys.mapper.ArchCostItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 数据字典Service
 * @author lgf
 * @version 2017-01-16
 */
@Service
@Transactional(readOnly = true)
public class ArchCostItemService extends CrudService<ArchCostItemMapper, ArchCostItem> {

	@Autowired
	private ArchCostItemMapper archCostItemMapper;

	/**
	 * 添加款项
	 */
	@Transactional(readOnly = false)
	public boolean setCost(String objectId, String [] ids) {
        int newNum = 0;
        ArchCostItem archCostItem;
        List<String> costList = archCostItemMapper.getCostList(objectId);
		for (int i = 0; i < ids.length; i++) {
		    if(costList.size() > 0){
                if(costList.contains(ids[i])){
                    continue;//已有款项直接跳过，不添加
                }else {
                    archCostItem = new ArchCostItem();
                    archCostItem.setCostitemId(ids[i]);
                    archCostItem.setObjectId(objectId);
                    archCostItem.setIn("1");
                    archCostItem.setOut("1");
                    archCostItem.setSelected("1");
                    archCostItem.setStatus("1");
                    archCostItem.setDelFlag("0");
                    archCostItem.preInsert();
                    archCostItemMapper.insertCost(archCostItem);
                    newNum++;
                }
            }else {
                archCostItem = new ArchCostItem();
                archCostItem.setCostitemId(ids[i]);
                archCostItem.setObjectId(objectId);
                archCostItem.setIn("1");
                archCostItem.setOut("1");
                archCostItem.setSelected("1");
                archCostItem.setStatus("1");
                archCostItem.setDelFlag("0");
                archCostItem.preInsert();
                archCostItemMapper.insertCost(archCostItem);
                newNum++;
            }

		}
		if(newNum > 0){
			return true;
		}else {
			return false;
		}
	}

    /**
     * 根据objectId获取档案对应的款项
     * @param objectId
     * @return
     */
	@Transactional(readOnly = false)
    public List<ArchCostItem> getCostList(String objectId){
	    ArchCostItem archCostItem = new ArchCostItem();
	    archCostItem.setObjectId(objectId);
	    return archCostItemMapper.findList1(archCostItem);
    }

	@Transactional(readOnly = false)
	public void deleteDictValue(String id) {
		archCostItemMapper.deleteCost(id);
	}

    /**
     * 根据单据获取对应的档案款项列表
     * @param page
     * @return
     */
    @Transactional(readOnly = false)
    public Page<ArchCostItem> findCostPage1(Page<ArchCostItem> page,String objectId) {
        ArchCostItem archCostItem = new ArchCostItem();
        dataRuleFilter(archCostItem);
        archCostItem.setPage(page);
        page.setList(archCostItemMapper.findList(archCostItem,objectId));
        return page;
    }

}